﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;

using System.Reflection;

using System.Threading;




/// <summary>
/// 网络事件
/// </summary>
public class NetEvent
{
    /// <summary>
    /// 连接失败
    /// </summary>
    public const string CONNECT_ERROR = "connectError";
    /// <summary>
    /// 连接成功
    /// </summary>
    public const string CONNECTED = "connected";
    /// <summary>
    /// socket断开连接
    /// </summary>
    public const string CLOSED = "serverClosed";

    /// <summary>
    /// 数据接收完成
    /// </summary>
    public const string RECEIVED = "received";

    /// <summary>
    /// 网络过慢
    /// </summary>
    public const string NET_LOWER = "net_lower";
    /// <summary>
    /// 服务端长时间无回应
    /// </summary>
    public const string NET_TIMEOUT = "net_timeout";


    /*  /// <summary>
      /// 发送数据失败
      /// </summary>
      public const string SEND_ERROR = "sendError";
      /// <summary>
      /// 接收数据失败
      /// </summary>
      public const string RECEIVE_ERROR = "receiveError";
      /// <summary>
      /// 执行send或者receive的时候socket尚未连接成功
      /// </summary>
      public const string SOCKET_NOT_CONNECTED = "socketNotConnected";*/
    protected int mType;
    protected string mMessage;
    protected byte[] mSource;
    public NetEvent(int type, string message = null, byte[] source = null)
    {
        mType = type;
        mMessage = message;
        mSource = source;
    }
    /// <summary>
    /// 事件类型
    /// </summary>
    public int Type
    {
        get
        {
            return mType;
        }

        set
        {
            mType = value;
        }
    }


    /// <summary>
    /// 抛出该事件的事件源
    /// </summary>
    public byte[] Source
    {
        get
        {
            return mSource;
        }
        set
        {
            mSource = value;
        }
    }

    /// <summary>
    /// 事件本身带出来的数据
    /// </summary>
    public string Message
    {
        get
        {
            return mMessage;
        }
        set
        {
            mMessage = value;
        }
    }



}



public static class ErrorCode
{
    /// <summary>
    /// 无任何错误，正常
    /// </summary>
    public const int NO_ERROR = -2;
    /// <summary>
    /// socket产生的未知错误
    /// </summary>
    public const int NET_SOCKET_UNKNOW_ERROR = -1;
    /// <summary>
    /// connect之前，打开socket失败
    /// </summary>
    public const int NET_SOCKET_OPEN_ERROR = 0;
    /// <summary>
    /// socket连接失败
    /// </summary>
    public const int NET_SOCKET_CONNECT_ERROR = 1;
    /// <summary>
    /// 发送数据失败
    /// </summary>
    public const int NET_SOCKET_SEND_ERROR = 2;
    /// <summary>
    /// 结束挂起的异步发送时错误
    /// </summary>
    public const int NET_SOCKET_END_SEND_ERROR = 3;
    /// <summary>
    /// 当发送数据时，socket还没连接
    /// </summary>
    public const int NET_SOCKET_NOT_CONNECTED_WHEN_SEND = 4;
    /// <summary>
    /// 接收数据失败
    /// </summary>
    public const int NET_SOCKET_RECEIVE_ERROR = 5;
    /// <summary>
    /// 结束挂起的异步接收时错误
    /// </summary>
    public const int NET_SOCKET_END_RECEIVE_ERROR = 6;
    /// <summary>
    /// 发送数据超时
    /// </summary>
    public const int NET_SOCKET_SEND_TIMEOUT = 7;
    /// <summary>
    /// 接受数据超时
    /// </summary>
    public const int NET_SOCKET_RECEIVE_TIMEOUT = 8;
    /// <summary>
    /// socket连接超时
    /// </summary>
    public const int NET_SOCKET_CONNECT_TIMEOUT = 9;

}

